<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客列表</title>
    <link rel="stylesheet" href="css/list.css">
    <link rel="stylesheet" href="css/blog_list.css">
    <script src="js/jquery.min.js"></script>
    <script src="js/jscommon.js"></script>
    <style>
        
        .nav{
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            height: 50px;
        }
        .container{
            padding-top: 80px;
            height: auto;
        }
        /* .container-right{
            width: auto;
        } */
        .blog-pagnation-wrapper{
            height: 40px;
            margin: 16px 0;
            text-align: center;
        }
        .blog-pagnation-item{
            display: inline-block;
            padding: 8px;
            border: 1px solid #d0d0d5;
            color: #333;
        }
        .blog-pagnation-item:hover{
            background: #4e4eeb;
            color: #fff;
        }
        .blog-pagnation-item.actvie{
            background: #4e4eeb;
            color: #fff;
        }

    </style>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/小灰灰.jpeg" alt="">
        <span class="title">我的博客系统</span>
        <span class="spacer"></span>
        <div class="row">
            <input type="text" id="size" placeholder="在这里设置博客数/页(3-15)">
            <a href="javascript:setSize()">设置</a>
        </div>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="myblog_list.html">我的主页</a>
        <a href="blog_add.html">写博客</a>
        <a href="login.html">登陆</a>
        <a href="javascript:logout()">注销</a>
    </div>
    <!-- 版心 -->
    <div class="container">
        <!-- 右侧内容详情 -->
        <div class="container-right">
            <div id="articleList" style="overflow: auto">
                <!-- 每一篇博客包含标题, 摘要, 时间 -->

            </div>
            
            <hr>
            <div class="blog-pagnation-wrapper">
                <button class="blog-pagnation-item" onclick="goFirstPage()">首页</button> 
                <button class="blog-pagnation-item" onclick="goBeforePage()">上一页</button>  
                <button class="blog-pagnation-item" onclick="goNextPage()">下一页</button>
                <button class="blog-pagnation-item" onclick="goLastPage()">末页</button>
            </div>
        </div>
    </div>
    <script>
        // 1. 获取当前页码
        var page = (getUrlValue("page")=="" ? 1 : getUrlValue("page"));
        // 2. 获取当前页面最大博客数，由于我们还没给用户设置选项，因此暂时默认设置
        // var size = getUrlValue("size");
        var size = (getUrlValue("size")=="" ? 3 : getUrlValue("size"));
        // 3. 定义全局页数，从 getArtList函数中获取
        var pageCount = 0;

        // 4. 先得到当前页面 url 的数据
        // 向后端发送请求，将结果进行展示
        function getArtList() {
            jQuery.ajax({
                url: "/art/getlist",
                type: "POST",
                data: {"page": page, "size": size},
                success: function(result) {
                    if(result != null && result.code == 200 && result.data.list.length > 0) {
                        // 正常查询结果返回
                        pageCount = result.data.pageCount;
                        var artListDiv = "";
                        for(var i = 0; i < result.data.list.length; i++) {
                            var artItem = result.data.list[i];
                            // 对内容进行拼接
                            artListDiv += '<div class="blog">';
                            artListDiv += '<div class="title">' + artItem.title + '</div>';
                            artListDiv += '<div class="date">' + artItem.createtime + "</div>";
                            artListDiv += '<div class="desc">';
                            artListDiv += artItem.content;
                            artListDiv += '</div>';
                            artListDiv += '<a href="blog_content.html?id=' + 
                                artItem.id + '" class="detail">查看全文 &gt;&gt;</a>';
                            artListDiv += '</div>';
                        }
                        jQuery("#articleList").html(artListDiv);
                    }
                }
            });
        } 
        getArtList();

        // 博客分页跳转
        // 转到首页
        function goFirstPage() {
            if(page <= 1) {
                alert("当前已经在首页！");
                return;
            }
            location.href = "blog_list.html?page=1" + "&size=" + size;
        }

        // 转到上一页
        function goBeforePage() {
            if(page <= 1) {
                alert("当前已经在首页！");
                return;
            }
            page = parseInt(page) - 1;
            location.href = "blog_list.html?page=" + page + "&size=" + size;
        }

        // 转到下一页
        function goNextPage() {
            if(page >= pageCount) {
                alert("已经是最后一页！");
                return;
            }
            page = parseInt(page) + 1;
            location.href = "blog_list.html?page=" + page + "&size=" + size;
        }

        // 转到最后一页
        function goLastPage() {
            if(page >= pageCount) {
                alert("已经是最后一页！");
                return;
            }
            location.href = "blog_list.html?page=" + pageCount + "&size=" + size;
        }

        // 设置size
        function setSize() {
            var inputSize = jQuery("#size");
            if(inputSize.val() == "") {
                alert("请先输入值！");
                inputSize.focus();
                return;
            }
            if(inputSize.val() < 3) {
                size = 3;
            }else if(inputSize.val() > 15) {
                size = 15;
            }else {
                size = inputSize.val();
            }
            location.href = "blog_list.html?page=" + page + "&size=" + size;
        }
    
    </script>
</body>
</html>